package dbu.dbase.version3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import dbu.dbase.DBaseStructure;
import dbu.dbase.DataTypes;

public class MdxTagHeader extends DBaseStructure {

	/** Pointer to root page */
	private int root;

	/** File size in pages */
	private int size;

	/** Key format: 00h - Right, left, dtoc; 08h - Descending order; 10h - Fields , string; 40h - Unique keys */
	private byte format;

	/** Key type: C - Character; N - Numerical; D - Date */
	private DataTypes type;

	/** Index key length */
	private short keyLength;

	/** Max.no.of keys/page */
	private short max;

	/** Secondary key type: 0 - Character ; 1 - Numerical/date */
	private short second;

	/** Index key item length */
	private short itemLength;

	/** Unique flag */
	private byte unique;

	/** Tag name */
	private String tagName;

	public int getRoot() {
		return root;
	}

	/**
	 * Read  
	 * @param stream
	 * @throws IOException
	 */
	@Override
	protected int read(InputStream stream) throws IOException {
		/** 4 byte */
		root = readInt(stream);
		/** 4 byte */
		size = readInt(stream);
		/** 1 byte */
		format = readByte(stream);
		/** 1 byte */
		type = DataTypes.getBySymbol((char) stream.read());
		/** 2 byte 	Reserved */
		readShort(stream);
		/** 2 byte */
		keyLength = readShort(stream);
		/** 2 byte */
		max = readShort(stream);
		/** 2 byte */
		second = readShort(stream);
		/** 2 byte */
		itemLength = readShort(stream);
		/** 3 byte	Reserved */
		readBuff(stream, 3);
		/** 1 byte */
		unique = readByte(stream);
		/** 11 bytes */
		tagName = readString(stream, 11);

		return 35;
	}

	/**
	 * Write 
	 * @param stream
	 * @throws IOException
	 */
	protected void write(OutputStream stream) throws IOException {
		/** 4 byte */
		stream.write(root);
		/** 4 byte */
		stream.write(size);
		/** 1 byte */
		stream.write(format);
		/** 1 byte */
		stream.write(type.getSymbol());
		/** 2 byte	Reserved */
		stream.write(new byte[] {0, 0});
		/** 2 byte */
		stream.write(keyLength);
		/** 2 byte */
		stream.write(max);
		/** 2 byte */
		stream.write(second);
		/** 2 byte */
		stream.write(itemLength);
		/** 3 byte	Reserved */
		stream.write(new byte[] {0, 0, 0});
		/** 1 byte */
		stream.write(unique);
		/** 11 bytes */
		writeString(stream, tagName, 11);
	}

	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		result.append("root:       "); result.append(root); result.append("\n");
		result.append("size:       "); result.append(size); result.append("\n");
		result.append("format:     "); result.append(format); result.append("\n");
		result.append("type:       "); result.append(type); result.append("\n");
		result.append("keyLength:  "); result.append(keyLength); result.append("\n");
		result.append("max:        "); result.append(max); result.append("\n");
		result.append("second:     "); result.append(second); result.append("\n");
		result.append("itemLength: "); result.append(itemLength); result.append("\n");
		result.append("unique:     "); result.append(unique); result.append("\n");
		result.append("tagName:    "); result.append(tagName); result.append("\n");
		return result.toString();
	}
}
